package com.bcntouch.touch2act.Suggest;

import android.content.Intent;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.NavUtils;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.bcntouch.touch2act.R;
import com.bcntouch.touch2act.Constants.Constants;
import com.bcntouch.touch2act.Opine.OpinionStorage;
import com.bcntouch.touch2act.Opine.OpinionStorage.OpinionMetaData;
import com.bcntouch.touch2act.Opine.OpinionTagDetectedActivity;

public class SuggestActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

	public final static String EXTRA_SUGGESTION_ID = Constants.AUTHORITY + ".SUGGESTION_ID";
	
	// This is the Adapter being used to display the list's data.
    SimpleCursorAdapter mAdapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggest);
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        
        OpinionStorage.initDefaultOpinions(this);
        
        ListView listView = (ListView) findViewById(R.id.suggestions);
        
        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new SimpleCursorAdapter(this,
                						   android.R.layout.simple_list_item_2,
                						   null,
                						   new String[] { OpinionMetaData.QUESTION, OpinionMetaData.CATEGORY },
                						   new int[] { android.R.id.text1, android.R.id.text2 },
                						   0);
        
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				CursorWrapper test = (CursorWrapper)mAdapter.getItem(position);
				String suggestionId = test.getString(test.getColumnIndex(OpinionMetaData._ID));
				onSuggestionSelected(suggestionId);
			}
		});
        
        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getSupportLoaderManager().initLoader(0, null, this);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.activity_suggest, menu);
//        return true;
//    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void onSuggestionSelected(String suggestionId) {
    	Intent intent = new Intent(this, OpinionTagDetectedActivity.class);
    	intent.putExtra(EXTRA_SUGGESTION_ID, suggestionId);
    	startActivity(intent);
    }
    
    public void onAddProposalButtonClick(View view) {
    	Intent intent = new Intent(this, SuggestionTypesActivity.class);
    	startActivity(intent);
    }
    
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return OpinionStorage.getAllOpinionsCursorLoader(this);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
        
//        if (data == null || data.getCount() < 1) {
//        	Intent intent = new Intent(this, SuggestionTypesActivity.class);
//        	startActivity(intent);
//        }
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		// This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
	}

}
